<template>
  <a-card title="控制器设置">
    <a-form-model ref="form" :model="model" :rules="validatorRules" :label-col="labelCol" :wrapper-col="wrapperCol">
      <!--      <a-card title="控制器数据" style="margin-bottom: 20px;">
            <a-row :gutter="24">
              <a-col :span="24">
                <a-form-model-item label="控制器数据" prop="controlJson" >
                  <a-textarea
                    v-model="model.controlJson"
                    placeholder="控制器数据"
                    :rows="10"
                    :disabled="true"
                    style=" display: block; width: 100%; border: 1px solid #d9d9d9; border-radius: 4px; font-size: 14px; color: #333; background-color: #f5f5f5;text-align: left;"
                  />
                </a-form-model-item>
              </a-col>
            </a-row>
            </a-card>-->

      <!--      <a-row :gutter="16">
              <a-col :span="24">
                <a-form-model-item label="预估里程" prop="mileage">
                  <a-input-number
                    v-model="model.mileage"
                    placeholder="请输入预估里程"
                    style="width: 100%; border: 1px solid #d9d9d9; border-radius: 4px;"
                  />
                </a-form-model-item>
              </a-col>
            </a-row>-->

      <a-card title="0xA10A 电机控制器尾灯设置" style="margin-bottom: 20px;">
        <a-form-model ref="a10aForm" :model="a10aModel" :label-col="labelCol" :wrapper-col="wrapperCol">
          <a-row :gutter="16">
            <a-col :span="12">
              <a-form-model-item label="尾灯闪烁使能" prop="tailLightEnabled">
                <a-switch v-model="a10aModel.tailLightEnabled" checked-children="开启" un-checked-children="关闭" />
              </a-form-model-item>
            </a-col>
            <a-col :span="12">
              <a-form-model-item label="大灯开关" prop="headlightEnabled">
                <a-switch v-model="a10aModel.headlightEnabled" checked-children="开启" un-checked-children="关闭" />
              </a-form-model-item>
            </a-col>
          </a-row>
          <a-row :gutter="16">
            <a-col :span="12">
              <a-form-model-item label="闪烁模式" prop="blinkMode">
                <a-select v-model="a10aModel.blinkMode" placeholder="请选择闪烁模式">
                  <a-select-option :value="0">闪烁</a-select-option>
                  <a-select-option :value="1">常亮</a-select-option>
                </a-select>
              </a-form-model-item>
            </a-col>
            <a-col :span="12">
              <a-form-model-item label="闪烁次数" prop="blinkTimes">
                <a-input-number style="width: -webkit-fill-available;" v-model="a10aModel.blinkTimes"
                                placeholder="请输入闪烁次数 (0 为持续闪烁)" :min="0" />
              </a-form-model-item>
            </a-col>
          </a-row>
          <a-button type="primary" @click="setControl('0xA10A')">设置 0xA10A</a-button>
        </a-form-model>
      </a-card>

      <a-card title="0xA109 极对数设置" style="margin-bottom: 20px;">
        <a-form-model ref="a109Form" :model="a109Model" :label-col="labelCol" :wrapper-col="wrapperCol">
          <a-row :gutter="16">
            <a-col :span="12">
              <a-form-model-item label="极对数" prop="polePairs">
                <a-input-number v-model="a109Model.polePairs" placeholder="请输入极对数"
                                style="width: 100%; border: 1px solid #d9d9d9; border-radius: 4px;" />
              </a-form-model-item>
            </a-col>
          </a-row>
          <a-button type="primary" @click="setControl('0xA109')">设置 0xA109</a-button>
        </a-form-model>
      </a-card>
      <a-card title="0xA10D 电池电压和限流值设置" style="margin-bottom: 20px;">
        <a-form-model ref="a10dForm" :model="a10dModel" :label-col="labelCol" :wrapper-col="wrapperCol">
          <a-row :gutter="16">
            <a-col :span="12">
              <a-form-model-item label="电池电压类型 (V)" prop="batteryVoltageType">
                <a-input-number v-model="a10dModel.batteryVoltageType" placeholder="请输入电池电压类型"
                                style="width: 100%; border: 1px solid #d9d9d9; border-radius: 4px;" />
              </a-form-model-item>
            </a-col>
            <a-col :span="12">
              <a-form-model-item label="限流值 (A)" prop="currentLimit">
                <a-input-number v-model="a10dModel.currentLimit" placeholder="请输入限流值"
                                style="width: 100%; border: 1px solid #d9d9d9; border-radius: 4px;" />
              </a-form-model-item>
            </a-col>
          </a-row>
          <a-button type="primary" @click="setControl('0xA10D')">设置 0xA10D</a-button>
        </a-form-model>
      </a-card>

      <a-card title="0xA100" style="margin-bottom: 20px;">
        <a-form-model ref="a100Form" :model="a100Model" :label-col="labelCol" :wrapper-col="wrapperCol">
          <a-row>

            <a-row :gutter="16">
              <a-col :span="12">
                <a-form-model-item label="电机锁" prop="motorLock">
                  <a-switch v-model="a100Model.motorLock" checked-children="上锁" un-checked-children="解锁" />
                </a-form-model-item>
              </a-col>
              <a-col :span="12">
                <a-form-model-item label="动力禁用" prop="powerDisable">
                  <a-switch v-model="a100Model.powerDisable" checked-children="禁用" un-checked-children="启用" />
                </a-form-model-item>
              </a-col>
            </a-row>
            <a-row :gutter="16">
              <a-col :span="12">
                <a-form-model-item label="转把关闭延时" prop="assistLevel">
                  <a-select v-model="a100Model.assistLevel" placeholder="请选择转把关闭延时">
                    <a-select-option :value="0">0 秒</a-select-option>
                    <a-select-option :value="1">1 秒</a-select-option>
                    <a-select-option :value="2">3 秒</a-select-option>
                    <a-select-option :value="3">5 秒</a-select-option>
                    <a-select-option :value="4">10 秒</a-select-option>
                    <a-select-option :value="5">15 秒</a-select-option>
                    <a-select-option :value="6">30 秒</a-select-option>
                    <a-select-option :value="7">60 秒</a-select-option>
                  </a-select>
                </a-form-model-item>
              </a-col>

              <a-col :span="12">
                <a-form-model-item label="国标速度" prop="speed">
                  <a-input-number
                    v-model="a100Model.speed"
                    placeholder="请输入国标速度"
                    style="width: 100%; border: 1px solid #d9d9d9; border-radius: 4px;"
                  />
                </a-form-model-item>
              </a-col>
            </a-row>

            <a-row :gutter="16">

              <a-col :span="12">
                <a-form-model-item label="工作模式" prop="workModeValue">
                  <a-select v-model="a100Model.workModeValue" placeholder="请选择工作模式">
                    <a-select-option :value="3">混合</a-select-option>
                    <a-select-option :value="2">纯电动</a-select-option>
                    <a-select-option :value="1">纯助力</a-select-option>
                  </a-select>
                </a-form-model-item>
              </a-col>
              <a-col :span="12">
                <a-form-model-item label="启动模式" prop="startModeValue">
                  <a-select v-model="a100Model.startModeValue" placeholder="请选择启动模式">
                    <a-select-option :value="0">零启动</a-select-option>
                    <a-select-option :value="1">非零启动</a-select-option>
                  </a-select>
                </a-form-model-item>
              </a-col>
            </a-row>

          </a-row>
          <a-button type="primary" @click="setControl('0xA100')">设置 0xA100</a-button>
        </a-form-model>
      </a-card>

      <a-card title="0xA10E" style="margin-bottom: 20px;">
        <a-form-model ref="a10eForm" :model="a10eModel" :label-col="labelCol" :wrapper-col="wrapperCol">
          <a-row>
            <a-row :gutter="16">
              <a-col :span="12">
                <a-form-model-item label="国标限速" prop="speedLimitEnabled">
                  <a-switch v-model="a10eModel.speedLimitEnabled" checked-children="开启" un-checked-children="关闭" />
                </a-form-model-item>
              </a-col>
              <a-col :span="12">
                <a-form-model-item label="联动功能" prop="linkageEnabled">
                  <a-switch v-model="a10eModel.linkageEnabled" checked-children="开启" un-checked-children="关闭" />
                </a-form-model-item>
              </a-col>
            </a-row>
            <a-row :gutter="16">
              <a-col :span="12">
                <a-form-model-item label="EABS" prop="eabsEnabled">
                  <a-switch v-model="a10eModel.eabsEnabled" checked-children="开启" un-checked-children="关闭" />
                </a-form-model-item>
              </a-col>

              <a-col :span="12">
                <a-form-model-item label="防陡坡" prop="slopePreventionEnabled">
                  <a-switch v-model="a10eModel.slopePreventionEnabled" checked-children="开启"
                            un-checked-children="关闭" />
                </a-form-model-item>
              </a-col>
            </a-row>
            <a-row :gutter="16">
              <a-col :span="12">
                <a-form-model-item label="TCS" prop="tcsEnabled">
                  <a-switch v-model="a10eModel.tcsEnabled" checked-children="开启" un-checked-children="关闭" />
                </a-form-model-item>
              </a-col>
              <a-col :span="12">
                <a-form-model-item label="启动档位" prop="startGear">
                  <a-select v-model="a10eModel.startGear" placeholder="请选择启动档位">
                    <a-select-option :value="0">0</a-select-option>
                    <a-select-option :value="1">1</a-select-option>
                    <a-select-option :value="2">2</a-select-option>
                  </a-select>
                </a-form-model-item>
              </a-col>
            </a-row>
            <a-row :gutter="16">
              <a-col :span="12">
                <a-form-model-item label="软启动等级" prop="softStartLevel">
                  <a-input-number v-model="a10eModel.softStartLevel" placeholder="请输入软启动等级 (0-31)" :min="0"
                                  :max="31" />
                </a-form-model-item>
              </a-col>

              <a-col :span="12">
                <a-form-model-item label="停车进入P档时间" prop="parkingToPTime">
                  <a-select v-model="a10eModel.parkingToPTime" placeholder="请选择停车进入P档时间">
                    <a-select-option :value="0">关闭功能</a-select-option>
                    <a-select-option :value="1">10秒进入P</a-select-option>
                    <a-select-option :value="2">30秒进入P</a-select-option>
                    <a-select-option :value="3">60秒进入P</a-select-option>
                    <a-select-option :value="4">130秒进入P</a-select-option>
                  </a-select>
                </a-form-model-item>
              </a-col>
            </a-row>
          </a-row>
          <a-button type="primary" @click="setControl('0xA10E')">设置 0xA10E</a-button>
        </a-form-model>
      </a-card>

      <a-card title="0xA10F 欠压保护电压设置" style="margin-bottom: 20px;">
        <a-form-model ref="a10fForm" :model="a10fModel" :label-col="labelCol" :wrapper-col="wrapperCol">
          <a-row :gutter="16">
            <a-col :span="12">
              <a-form-model-item label="欠压保护电压 (0.1V)" prop="undervoltageProtection">
                <a-input-number
                  v-model="a10fModel.undervoltageProtection"
                  placeholder="请输入欠压保护电压"
                  style="width: 100%; border: 1px solid #d9d9d9; border-radius: 4px;"
                />
              </a-form-model-item>
            </a-col>
          </a-row>
          <a-button type="primary" @click="setControl('0xA10F')">设置 0xA10F</a-button>
        </a-form-model>
      </a-card>
      <a-card title="0xA110" style="margin-bottom: 20px;">
        <a-form-model ref="a110Form" :model="a110Model" :label-col="labelCol" :wrapper-col="wrapperCol">
          <a-row>
            <a-row :gutter="16">

              <a-col :span="12">
                <a-form-model-item label="刹车能回收开关" prop="brakeEnergyRecoveryEnabled">
                  <a-switch v-model="a110Model.brakeEnergyRecoveryEnabled" checked-children="开启"
                            un-checked-children="关闭" />
                </a-form-model-item>
              </a-col>
              <a-col :span="12">
                <a-form-model-item label="转吧刹车能开关" prop="handleBrakeEnergyRecoveryEnabled">
                  <a-switch v-model="a110Model.handleBrakeEnergyRecoveryEnabled" checked-children="开启"
                            un-checked-children="关闭" />
                </a-form-model-item>
              </a-col>

              </a-row>
            <a-row :gutter="16">

              <a-col :span="12">
                <a-form-model-item label="刹把解P" prop="handleReleasePowerEnabled">
                  <a-switch v-model="a110Model.handleReleasePowerEnabled" checked-children="关闭"
                            un-checked-children="开启" />
                </a-form-model-item>
              </a-col>
              <a-col :span="12">
                <a-form-model-item label="转把定速开启" prop="handleCruiseControlEnabled">
                  <a-switch v-model="a110Model.handleCruiseControlEnabled" checked-children="开启"
                            un-checked-children="关闭" />
                </a-form-model-item>
              </a-col>


              </a-row>
            <a-row :gutter="16">
            </a-row>
            <a-col :span="12">
              <a-form-model-item label="按键定速开启" prop="buttonCruiseControlEnabled">
                <a-switch v-model="a110Model.buttonCruiseControlEnabled" checked-children="开启"
                          un-checked-children="关闭" />
              </a-form-model-item>
            </a-col>

          </a-row>
            <a-row :gutter="16">

              <a-col :span="12">
                <a-form-model-item label="助力强度" prop="assistIntensity">
                  <a-select v-model="a110Model.assistIntensity" placeholder="请选择助力强度">
                    <a-select-option :value="0">000 (关闭助力)</a-select-option>
                    <a-select-option :value="1">001</a-select-option>
                    <a-select-option :value="3">011</a-select-option>
                    <a-select-option :value="7">111</a-select-option>
                    <a-select-option :value="6">110</a-select-option>
                    <a-select-option :value="4">100</a-select-option>
                  </a-select>
                </a-form-model-item>
              </a-col>
              <a-col :span="12">
                <a-form-model-item label="刹车能回收强度" prop="recoveryIntensity">
                  <a-select v-model="a110Model.recoveryIntensity" placeholder="请选择刹车能回收强度">
                    <a-select-option :value="0">0</a-select-option>
                    <a-select-option :value="1">1</a-select-option>
                    <a-select-option :value="2">2</a-select-option>
                    <a-select-option :value="3">3</a-select-option>
                    <a-select-option :value="4">4</a-select-option>
                    <a-select-option :value="5">5</a-select-option>
                  </a-select>
                </a-form-model-item>
              </a-col>

              </a-row>

            <a-button type="primary" @click="setControl('0xA110')">设置 0xA110</a-button>
        </a-form-model>
      </a-card>

      <a-card title="0xA113 速度百分比设置" style="margin-bottom: 20px;">
        <a-form-model ref="a113Form" :model="a113Model" :label-col="labelCol" :wrapper-col="wrapperCol">
          <a-row :gutter="16">
            <a-col :span="12">
              <a-form-model-item label="一档速度百分比" prop="gear1Percentage">
                <a-input-number
                  v-model="a113Model.gear1Percentage"
                  placeholder="请输入一档速度百分比"
                  style="width: 100%; border: 1px solid #d9d9d9; border-radius: 4px;"
                />
              </a-form-model-item>
            </a-col>
            <a-col :span="12">
              <a-form-model-item label="二档速度百分比" prop="gear2Percentage">
                <a-input-number
                  v-model="a113Model.gear2Percentage"
                  placeholder="请输入二档速度百分比"
                  style="width: 100%; border: 1px solid #d9d9d9; border-radius: 4px;"
                />
              </a-form-model-item>
            </a-col>
          </a-row>
          <a-button type="primary" @click="setControl('0xA113')">设置 0xA113</a-button>
        </a-form-model>
      </a-card>

      <a-row :gutter="16">
        <a-col :span="10">
          <a-button type="primary" @click="queryController">查询控制器</a-button>
        </a-col>
      </a-row>
    </a-form-model>
  </a-card>
</template>

<script>
import { httpAction } from '@/api/manage'

export default {
  name: 'ControllerSettings',
  props: {
    carId: {
      type: Number,
      required: true
    }
  },
  data() {
    return {
      model: {
        controlJson: '',
        mileage: null
      },
      a100Model: {
        assistLevel: 3,
        motorLock: true,
        powerDisable: true,
        speed: 25,
        workModeValue: 1,
        startModeValue: 1
      },
      a10aModel: {
        tailLightEnabled: false,
        headlightEnabled: false,
        blinkMode: 0,
        blinkTimes: 0
      },
      a10eModel: {
        speedLimitEnabled: false,
        linkageEnabled: false,
        eabsEnabled: false,
        parkingToPTime: 0,
        slopePreventionEnabled: false,
        tcsEnabled: false,
        startGear: 0,
        softStartLevel: 0
      },
      a10fModel: {
        undervoltageProtection: null
      },
      a110Model: {
        brakeEnergyRecoveryEnabled: true, // 刹车能回收开关
        handleBrakeEnergyRecoveryEnabled: false, // 转吧刹车能开关
        handleReleasePowerEnabled: false, // 刹把解P
        recoveryIntensity: 0, // 刹车能回收强度
        assistIntensity: 3, // 助力强度 (对应默认值 111)
        handleCruiseControlEnabled: false, // 转把定速开启
        buttonCruiseControlEnabled: true // 按键定速开启
      },
      a113Model: {
        gear1Percentage: null,
        gear2Percentage: null
      },
      a109Model: {
        polePairs: null
      },
      a10dModel: {
        batteryVoltageType: null,
        currentLimit: null
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 8 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      validatorRules: {},
      url: {
        queryControl: '/rental/ebike/queryControl',
        setControl: '/rental/ebike/setControl'
      }
    }
  },
  methods: {
    queryController() {
      const terminalId = this.carId // 使用 props 传入的 carId

      this.confirmLoading = true

      httpAction(this.url.queryControl + `?terminalId=${terminalId}`, {}, 'get')
        .then((res) => {
          if (res.success) {
            this.$message.success('查询控制器成功')
            // 处理响应数据 (res.result) 并更新 model.controlJson
            this.model.controlJson = JSON.stringify(res.result, null, 2) // 格式化 JSON 数据
          } else {
            this.$message.warning(res.message)
          }
        })
        .catch((error) => {
          this.$message.error('查询控制器失败：' + error)
        })
        .finally(() => {
          this.confirmLoading = false
        })
    },

    setControl(address) {
      const that = this
      let model = {}
      switch (address) {
        case '0xA100':
          model = this.a100Model
          break
        case '0xA10A':
          model = this.a10aModel
          break
        case '0xA10E':
          model = this.a10eModel
          break
        case '0xA10F':
          model = this.a10fModel
          break
        case '0xA113':
          model = this.a113Model
          break
        case '0xA110':
          model = this.a110Model
          break
        case '0xA109':
          model = this.a109Model
          break
        case '0xA10D':
          model = this.a10dModel
          break
        default:
          break
      }

      that.confirmLoading = true
      httpAction(this.url.setControl, { carId: this.carId, address, ...model }, 'post')
        .then((res) => {
          if (res.success) {
            that.$message.success('设置控制器成功')
          } else {
            that.$message.warning(res.message)
          }
        })
        .finally(() => {
          that.confirmLoading = false
        })
    }
  }
}
</script>